package com.gzwh.v1.sysapi.conntroller.member;

import com.gzwh.v1.sysapi.entity.application.SystemApplication;
import com.gzwh.v1.sysapi.entity.member.MemberMenuEntity;
import com.gzwh.v1.sysapi.entity.member.SFMemberEntity;
import com.gzwh.v1.sysapi.server.member.ApplicationRegServer;
import com.gzwh.v1.sysapi.server.member.ApplicationServer;
import com.gzwh.v1.sysapi.utils.JWTUtil;
import com.gzwh.v1.sysapi.utils.ReturnUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@CrossOrigin(maxAge = 36000)
@RestController
@RequestMapping("/api/member")
public class MemberIndexController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private ApplicationServer applicationServer;

    @Autowired
    private ApplicationRegServer applicationRegServer;

    //主页页面
    @RequestMapping("/index")
    @RequiresAuthentication
    public ModelMap index() {
        SFMemberEntity user = (SFMemberEntity) SecurityUtils.getSubject().getPrincipal();//获取用户

        return ReturnUtil.Success("获取完成", null, null);
    }

    //创建商铺
    @RequestMapping("create/shop")
    @RequiresAuthentication
    public ModelMap createShop() {
        SFMemberEntity user = (SFMemberEntity) SecurityUtils.getSubject().getPrincipal();//获取用户

        return ReturnUtil.Success("获取完成", null, null);
    }

    //检查商铺是否存在
    @RequestMapping("check/shop")
    public ModelMap checkShop() {
        //检查用户是否能创建商户
        SFMemberEntity user = (SFMemberEntity) SecurityUtils.getSubject().getPrincipal();//获取用户

        return ReturnUtil.Success("获取完成", null, null);
    }

    //检查是否开通试卷权限
    @RequestMapping("menu")
    @RequiresAuthentication
    public ModelMap getMenu() {
        List<MemberMenuEntity> menuEntityList = new ArrayList<>();
        menuEntityList.add(new MemberMenuEntity(1, "/home", "Home", "首页", "icd", 0));
        menuEntityList.add(new MemberMenuEntity(2, "/Test1", "Test1", "菜单1", "icd", 0));
        menuEntityList.add(new MemberMenuEntity(3, "/TestT2", "TestT2", "菜单1的子菜单1", "icd", 2));
        menuEntityList.add(new MemberMenuEntity(4, "/TestT3", "TestT3", "菜单1的子菜单2", "icd", 2));
        menuEntityList.add(new MemberMenuEntity(5, "/TestT4", "TestT4", "菜单1的子菜单3", "icd", 2));
        menuEntityList.add(new MemberMenuEntity(6, "/Test2", "Test2", "菜单2", "icd", 0));
        menuEntityList.add(new MemberMenuEntity(3, "/Test2T2", "TestT2", "菜单2的子菜单1", "icd", 6));
        menuEntityList.add(new MemberMenuEntity(3, "/Test2T3", "TestT3", "菜单2的子菜单2", "icd", 6));
        return ReturnUtil.Success("获取完成", menuEntityList, null);
    }

    /**
     * 获取已授权应用。
     * 应用间使用通讯授权码进行通讯设置。
     * 目前开通为网络订餐，考试系统。
     * <p>
     * 需要判断用户是否拥有创建应用权限
     * <p>
     * 对用户的管理
     * <p>
     * 时间：2019-07-30
     *
     * @return
     */
    @RequestMapping("/application/create")
    @RequiresAuthentication
    public ModelMap createApplication(@RequestParam("aid") Long aid) {
        SFMemberEntity user = (SFMemberEntity) SecurityUtils.getSubject().getPrincipal();//获取用户
        ModelMap modelMap = checkAllowApplication();  //检查是否允许创建应用
        if (modelMap != null)
            return modelMap;
        //添加管理系统
        logger.info("当前用户【 " + user.getName() + " 】,正在进行应用创建，当前创建应用id为【 " + aid + " 】");
        boolean status = applicationRegServer.allowCheckUserCreateByUid(user.getMemberId(), aid);
        if (status) {
            //允许创建应用
            applicationRegServer.insertAppReg(user.getMemberId(), aid);
            return ReturnUtil.Success("用户创建应用成功", null, null);
        } else {
            logger.info("当前用户【" + user.getName() + "】，创建应用上限");
            return ReturnUtil.Success("创建应用上限", null, null);
        }
    }

    /**
     * 获取应用列表
     *
     * @return
     */
    @RequestMapping("/application/get")
    @RequiresAuthentication
    public ModelMap getApplication() {
        ModelMap modelMap = checkAllowApplication();
        if (modelMap != null)
            return modelMap;
        List<SystemApplication> systemApplication = applicationServer.findSysApp();
        return ReturnUtil.Success("获取完成", systemApplication);
    }


    /**
     * 修改app应用
     *
     * @return
     */
    @RequestMapping("application/mod")
    @RequiresAuthentication
    public ModelMap modApplication() {
        ModelMap modelMap = checkAllowApplication();
        if (modelMap != null)
            return modelMap;
        return ReturnUtil.Success("获取完成");
    }

    /**
     * 暂时废弃
     *
     * @return
     */
    @RequestMapping("member/dd")
    @RequiresAuthentication
    public ModelMap getApplicationNum() {

        return ReturnUtil.Success("获取完成");
    }

    @RequestMapping("system/{id}/test")
    public ModelMap test(@PathVariable("id") String id) {
        System.out.println(id);
        return ReturnUtil.Success("获取完成");
    }

    /**
     * 查询用户允许创建应用权限，返回ModelMap,则失败返回错误消息。
     * 返回null，则检验通过
     * <p>
     * 时间： 2019-08-04
     *
     * @return
     */
    private ModelMap checkAllowApplication() {
        SFMemberEntity user = (SFMemberEntity) SecurityUtils.getSubject().getPrincipal();//获取用户
        if (user.getAppStatus() == 0) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("code", 100);
            hashMap.put("msg", "User.application_status is 0");
            logger.info("对用户[" + user.getName() + "]进行创建应用检验操作..验证未通过,app_status标志为0");
            return ReturnUtil.Error("当前用户无法创建应用", hashMap, "100");
        }
        return null;
    }
}
